//
//  CZCategoryViewModel.m
//  QQ空间
//
//  Created by zhangping on 16/2/7.
//  Copyright © 2016年 zhangping. All rights reserved.
//

#import "CZCategoryViewModel.h"
#import "CZCategoryModel.h"

@implementation CZCategoryViewModel
@synthesize categories = _categories;

/// 对外是不可变数组,不让别人来添加或删除数据
- (NSArray *)categories {
    if (_categories == nil) {
        // 创建可变数组
        NSMutableArray *categoriesM = [[NSMutableArray alloc] init];
        
        NSString *path = [[NSBundle mainBundle] pathForResource:@"categories" ofType:@"plist"];
        
        // 从文件加载分类数据
        NSArray *array = [NSArray arrayWithContentsOfFile:path];
        
        for (NSDictionary *dict in array) {
            // 添加模型数据
            CZCategoryModel *model = [[CZCategoryModel alloc] initWithDict:dict];
            [categoriesM addObject:model];
        }
        
        // 将转换好的模型数据赋值给不可变的 _categories
        _categories = categoriesM;
    }
    
    return _categories;
}
@end
